﻿<%@ Page Title="我的配货" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/shelf/base.css" rel="stylesheet">
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/img/validate/validate.css" rel="stylesheet">
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />
    <link href="../assets/css/plugins/jQuery-DataTables/css/dataTables.bootstrap.min.css" rel="stylesheet" />
    <style type="text/css">
        .system-box .right-side {
            width: 100%;
        }

        .condition, .condition-title {
            border-bottom: 1px solid #dfdfdd;
        }

            .condition .form-control {
                padding: 2px 6px;
            }

        .module-info td .control-label {
            margin-right: 0px;
            width: 70px;
        }

        #DateBegin, #DateEnd {
            width: 100px;
            background-color: white;
        }

        .search-TaskFlagName {
            float: left;
            margin-top: 4px;
        }

        .inmodal .modal-body {
            padding: 0px !important;
        }

        table.dataTable {
            margin-top: 0px !important;
        }

        #tableDetail_wrapper > .row > .col-sm-6 {
            display: none;
        }

        #tableDetail_wrapper td {
            padding: 0px 5px 0px 5px !important;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container">
                <div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
                    <form id="checkModuleForm" method="get" action="#">
                        <div class="modal-dialog">
                            <div class="modal-content animated bounceInRight">
                                <div class="modal-header">
                                    <span class="editTitle"></span>
                                    <button type="button" class="close" data-dismiss="modal">
                                        <span aria-hidden="true">&times;</span>
                                        <span class="sr-only">关闭</span>
                                    </button>
                                </div>
                                <div id="message" style="display: none"></div>
                                <div class="modal-body">
                                    <table id="tableDetail" class="table table-bordered  " width="100%">
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="condition">
                <div class="form-group i-checks">
                    <label>配货方式：</label>
                    <label>
                        <input type="radio" name="TaskType" id="search-TaskType1" data-value="1" checked="checked" />
                        可用库存</label>
                    <label>
                        <input type="radio" name="TaskType" id="search-TaskType2" data-value="2" />
                        成品外发到货通知单</label>
                    <label>| </label>
                    <label>状态：</label>
                    <select class="form-control input-w110" id="search-TaskFlag" name="search-TaskFlag">
                        <option value="0">---请选择---</option>
                        <option value="1">未完成</option>
                        <option value="2">草稿</option>
                        <option value="3">ERP生效</option>
                        <option value="4">未生效</option>
                        <option value="-1">作废</option>
                    </select>
                    <label>| </label>
                    <label class="control-label">配货时间</label>
                    <input name="plan-month" placeholder="开始时间" class="form-control layer-date" id="DateBegin" readonly="readonly" />
                    <label>到 </label>
                    <input name="plan-month" placeholder="结束时间" class="form-control layer-date" id="DateEnd" readonly="readonly" />
                </div>
            </div>
            <div class="right-content-box">
                <div class="ibox-content">
                    <table class="footable table table-stripped table-list loadTable">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>
            <div class="pageControl"></div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/img/validate/learunui-validator.js"></script>
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>
    <script src="../assets/js/plugins/layer/laydate/laydate.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/contabsInIframe.min.js"></script>
    <script src="../assets/js/plugins/jQuery-DataTables/jquery.dataTables.min.js"></script>
    <script src="../assets/js/plugins/jQuery-DataTables/dataTables.bootstrap.js"></script>
    <script src="../assets/js/plugins/linq/jquery.linq.min.js"></script>
    <script>
        Date.prototype.Format2 = function (fmt) { //author: meizz 
            var o = {
                "M+": this.getMonth() + 1, //月份 
                "d+": this.getDate(), //日 
                "h+": this.getHours(), //小时 
                "m+": this.getMinutes(), //分 
                "s+": this.getSeconds(), //秒 
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                "S": this.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

        var FormatDate = function (d) {
            if (d == null || d == undefined || d == "" || d == "0001-01-01T00:00:00") {
                return "";
            }
            return new Date(d).Format2("yyyy-MM-dd hh:mm");
        }

        var formatNowDate = function (days) {
            var date = new Date();
            date.setDate(date.getDate() + days);
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        };

        var GetTaskFlagName = function (taskFlag) {
            switch (taskFlag) {
                case 1:
                    return "未完成";
                case 2:
                    return "草稿";
                case 3:
                    return "ERP生效";
                case 4:
                    return "未生效";
                case -1:
                    return "作废";
                default:
                    return "";
            }
        };

        var GetTaskFlagColor = function (TaskFlag) {
            switch (TaskFlag) {
                case 1:
                    return "label label-default";
                case 2:
                    return "label label label-primary";
                case 3:
                    return "label label-success";
                case 4:
                    return "label label-warning";
                case -1:
                    return "label label-danger";
                default:
                    return "";
            }
        };

        var GetTaskTypeName = function (TaskType) {
            switch (TaskType) {
                case "1":
                    return "可用库存";
                case "2":
                    return "成品外发到货通知单";
                default:
                    return "";
            }
        };



        $('document').ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //当前页数
            var page = 1;
            commonUtil.loadBtnList(ModuleID, function () {
                $('#btnDetail').attr("disabled", "disabled");
                $('#btnTrash').attr("disabled", "disabled");
                rScroll();
            });

            $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green" });
            //初始化日期
            var DateBegin = {
                elem: "#DateBegin",
                format: "YYYY-MM-DD",
                min: "2015-12-1",
                max: "2099-12-31",
                istime: true,
                istoday: false
            };
            var DateEnd = {
                elem: "#DateEnd",
                format: "YYYY-MM-DD",
                min: "2015-12-1",
                max: "2099-12-31",
                istime: true,
                istoday: false
            };
            laydate(DateBegin);
            laydate(DateEnd);
            $('#DateBegin').val(formatNowDate(-7));
            $('#DateEnd').val(formatNowDate(0));

            var _TaskBillNo = "";
            var _StockID = "";
            var _StockName = "";
            var _TaskFlag = "";
            //处理表头
            if ($('.footable thead').children().size() == 0) {
                tableControl.initThead('.footable',
                    [
                        {
                            trClass: '',
                            children: [
                                { tdText: '公司代码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '配货单号', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '配货方式ID', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '配货方式', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '操作员', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '操作时间', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '完成时间', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '生成ERP单据时间', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                { tdText: '任务状态ID', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                { tdText: '任务状态', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' }
                            ]
                        }
                    ]);
            }

            var pageList = [];
            var loadList = function () {
                pageControl.init('DCTaskMaste', 'GetList', {
                    TaskType: $('input:radio[name="TaskType"]:checked').attr("data-Value"),
                    TaskFlag: $('#search-TaskFlag').val(),
                    DateBegin: $('#DateBegin').val() + " 00:00:00",
                    DateEnd: $('#DateEnd').val() + " 23:59:59",
                    Page: page,
                    Size: 100,
                    needLayer: true,
                    alwaysCallBack: function () {
                        $('#pageList').empty();
                    }
                }, function (result) {
                    pageList = [];
                    page = result.page;
                    var tBody = [];
                    if (!Y.checkNull(result.data)) {
                        if (result.data.Items.length > 0) {
                            result.data.Items.map(function (item, i) {
                                pageList.push(item);

                                console.log(item.PopularRate);

                                var tempTr = {
                                    trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                    children: [
                                        { tdText: (item.StockID || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'StockID hide' },
                                        { tdText: (item.StockName || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'StockName hide' },
                                        { tdText: (item.CompanyID || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'CompanyID' },
                                        { tdText: (item.TaskBillNo || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'TaskBillNo' },
                                        { tdText: (item.TaskType || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'TaskType hide' },
                                        { tdText: (GetTaskTypeName(item.TaskType) || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'TaskType' },
                                        { tdText: (item.OperatorName || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'Operator' },
                                        { tdText: FormatDate(item.OperateDate), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'OperateDate' },
                                        { tdText: FormatDate(item.Finishdate), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'Finishdate' },
                                        { tdText: FormatDate(item.PushBillDate), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'PushBillDate' },
                                        { tdText: (item.TaskFlag || ''), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'TaskFlag hide' },
                                        { tdText: '', c: 0, r: 0, tdHtml: '<label class="' + GetTaskFlagColor(item.TaskFlag) + '">' + GetTaskFlagName(item.TaskFlag) + '</label>', tdId: '', tdClass: 'TaskFlagName' }

                                    ]
                                };
                                tBody.push(tempTr);
                            });
                        }
                        rScroll();
                    }
                    tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 13, null);

                    _TaskBillNo = "";
                    _StockID = "";
                    _StockName = "";
                    _TaskFlag = "";
                    $('#btnDetail').attr('disabled', 'disabled');
                    $('#btnTrash').attr("disabled", "disabled");
                });
            }
            loadList();


            //选中记录
            $('#pageList').on('click', 'tr', function () {
                _TaskBillNo = $(this).find('.TaskBillNo').text();
                _StockID = $(this).find('.StockID').text();
                _StockName = $(this).find('.StockName').text();
                _TaskFlag = $(this).find('.TaskFlag').text();
                var _TaskFlagName = $(this).find('.TaskFlagName').text();


                if (_TaskFlagName == "作废") {
                    $('#btnGenerateERP').attr("disabled", "disabled");
                    $('#btnTrash').attr("disabled", "disabled");
                } else {
                    $('#btnGenerateERP').removeAttr("disabled");
                    $('#btnTrash').removeAttr("disabled");
                }

                //作废
                if (_TaskBillNo != "" && _TaskFlagName != "ERP生效" && _TaskFlagName != "未生效") {
                    $('#btnTrash').removeAttr('disabled');
                } else {
                    $('#btnTrash').attr("disabled", "disabled");
                }
                //明细
                if (_TaskBillNo != "") {
                    $('#btnDetail').removeAttr('disabled');
                } else {
                    $('#btnDetail').attr('disabled', 'disabled');
                }

                $('.footable tr').removeClass('check');
                $(this).addClass('check');
            });

            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                page = 1;
                loadList();
            });

            //生成ERP单据
            $('.btns-container').on('click', '#btnGenerateERP', function () {
                //先关闭已开的编辑窗口
                var _title = "配货作业";
                commonUtil.closeRepeatWindow(_title);

                //查找菜单
                var obj = $(".J_menuItem:contains('" + _title + "')", window.parent.document).map(function () {
                    if ($(this).text() == _title) {
                        return this;
                    }
                });
                var index = $(obj).attr("data-index");
                //配货作业

                var href = obj.attr("href");
                var OutStockID = _StockID;
                var TaskBillNo = _TaskBillNo;
                var StockName = _StockName;
                var TaskType = $("input[name='TaskType']:checked").attr("data-Value");
                var TaskFlag = _TaskFlag;
                if (!Y.checkNull(OutStockID) && !Y.checkNull(TaskBillNo) && !Y.checkNull(StockName) && !Y.checkNull(TaskType) && !Y.checkNull(TaskFlag)) {

                    $.OpenUrlInIFrame(href, index, obj.text(), "&step=3&TaskBillNo=" + TaskBillNo + "&OutStockID=" + OutStockID + "&TaskType=" + TaskType + "&TaskFlag=" + TaskFlag + "&StockName=" + escape(StockName));
                } else {
                    swal("请选择行！");
                }


            });

            //作废按钮
            $('.btns-container').on('click', '#btnTrash', function () {

                if (_TaskBillNo == "") {
                    swal("请先点击选择要作废的记录", '', 'error');
                    return false;
                }
                var doIt = false;
                swal({
                    title: "您确定要作废[" + _TaskBillNo + "]吗？",
                    text: "本操作属于危险操作，请认真考虑清楚",
                    type: "warning",
                    showCancelButton: true,
                    closeOnConfirm: false,
                    confirmButtonText: "我确定要作废",
                    cancelButtonText: "我再考虑一下",
                    confirmButtonColor: "#ec6c62"
                }, function () {
                    doIt = true;
                    layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                    Y.API2('DCTaskMaste', "Update",
                        {
                            TaskBillNo: _TaskBillNo
                        },
                        function (data) {
                            layer.closeAll('dialog');
                            if (data.Success == false) {
                                swal(data.Message, '', 'error');
                            } else {
                                swal("操作成功！", '', 'success');
                                page = 1;
                                loadList();
                            }
                        });

                });




            });


            var tableDetailHeight = 250;
            // 抢货作业  抢货
            var tableDetail;
            function loadTableDetail(data) {
                var opts = {
                    scrollY: tableDetailHeight,
                    language: { url: "../assets/js/plugins/jQuery-DataTables/i18n/Chinese.json" }, scrollX: true, searching: false, paging: false, info: false, ordering: false, lengthChange: false, processing: true,

                    columnDefs: [
                        {//==============================================分组分组分组分组分组分组分组分组分组分组分组=================================================================================
                            "targets": [0, 1, 2],//要合并的字段
                            "searchable": false,
                            createdCell: function (td, cellData, rowData, row, col) {
                                var table = tableDetail;//当前表格对象
                                var gcol = [{ colName: 1, group: 1, baseGroup: 0 }, { colName: 2, group: 1, baseGroup: 0 }];//要合并的组的条件，colName为对应的字段index，group：分组（1，2，3...） baseGroup:（父id）必须存在targets 中
                                $(table.cell(row, col).nodes()).addClass("groupbyColumn-" + row + " groupbyColumn-" + row + "-" + col);
                                var condition = false;
                                if (row != 0) {
                                    var g = $.Enumerable.From(gcol).GroupBy("$.group", "", function (key, e) {
                                        //   //console.log(key)
                                        return { group: key, info: e.Select('{"colName":$.colName,"baseGroup":$.baseGroup}').ToArray() };
                                    }, "$.toString()");

                                    //判断是否作为条件
                                    var isGroup = g.Count(function (arr) {
                                        var b = false;
                                        $.each(arr.info, function (i, obj) {
                                            if (obj.colName == col) {
                                                b = true;
                                            }
                                        });
                                        return b;
                                    }) > 0;
                                    // //console.log(col + ":" + isGroup);
                                    g.ForEach(function (arr) {
                                        var e = $.Enumerable.From(arr.info);
                                        if (e.Count(function (info) { return info.colName == col }) > 0) {
                                            e.ForEach(function (obj) {
                                                if (table.cell(row, obj.colName).data() != table.cell(row - 1, obj.colName).data()) {
                                                    //遍历所有条件
                                                    condition = true;
                                                }
                                                if (obj.baseGroup != 0) {//是否根级
                                                    nextGroup(obj)
                                                }
                                            });
                                        }
                                        if (!isGroup) {//不参数与条件筛选的也要和第一组一样
                                            e.ForEach(function (obj) {
                                                if (obj.baseGroup == 0) {
                                                    if (table.cell(row, obj.colName).data() != table.cell(row - 1, obj.colName).data()) {
                                                        condition = true;
                                                    }
                                                }

                                            });
                                        }
                                    });
                                    function nextGroup(obj) {//迭代下级
                                        g.Where(function (arr) {//查找上级
                                            return arr.group == obj.baseGroup;
                                        }).ForEach(function (arr2) {
                                            var e2 = $.Enumerable.From(arr2.info);
                                            e2.ForEach(function (obj2) {
                                                if (table.cell(row, obj2.colName).data() != table.cell(row - 1, obj2.colName).data()) {
                                                    condition = true;
                                                }
                                                nextGroup(obj2);
                                            });
                                        });

                                    }

                                }

                                var start = 0;
                                for (var i = row; i >= 0; i--) {

                                    var _starcol = $(table.cell(i, col).node()).attr("data-groupStart");

                                    if (_starcol > 0) {
                                        start = _starcol;
                                        break;
                                    }
                                }
                                //   var start =
                                if (condition) {
                                    ////console.log("row - groupIndex - 1")
                                    ////console.log(row - groupIndex - 1)
                                    $(td).attr("data-groupStart", row);// $("td:[data-groupStart]").attr("data-groupStart").attr();

                                    $(table.cell(start, col).node()).prop("rowspan", row - start);
                                    //  groupIndex = 0;//组index重新开始
                                } else {
                                    if (row == 0)//给第一行标记
                                    {
                                        $(td).attr("data-groupStart", row);
                                    } else {
                                        $(td).addClass("hide");
                                    }
                                    if (table.data().length - 1 == row)//最后一行
                                    {
                                        $(table.cell(start, col).node()).prop("rowspan", table.data().length - start);
                                    }
                                }



                                if (table.cell(row - 1, col).data() == cellData) {
                                    //   $(td).prop("rowspan", 2);
                                }

                            }
                            // ===============================================end分组分组分组分组分组分组分组分组分组分组分组end===============================================================================
                        },

                    ],
                    columns: [
                        {
                            data: null, Sortable: false, targets: 0, orderable: false, "name": "checkbox",
                            title: '<input type="checkbox" name="checkAllDetail" class="checkAll" value="all" />', className: "text-center hide",
                            render: function (data, type, row, meta) {
                                return '<input type="checkbox" name="checkItemDetail" class="checkItem" value="' + row.MaterialID + '" />';
                            },
                        },
                        { name: "ShopName", title: "店铺", data: "ShopName", className: "text-center", defaultContent: "" },
                        { name: "MaterialID", title: "货号", data: "MaterialID", className: "text-center", defaultContent: "" },
                        //{ name: "MaterialShortName", title: "货号简称", data: "MaterialShortName", className: "text-center", defaultContent: "" },
                        { title: "尺码", data: "SizeName", name: "SizeName", className: "text-center", defaultContent: "" },
                        { title: "数量", data: "Qty", name: "Qty", className: "text-center", defaultContent: "" },
                        { title: "抢货结果", data: "ActualQty", name: "ActualQty", className: "text-center", defaultContent: "" }
                    ]
                };

                var _url = "/api.ashx?c=PickingOperations&a=GetShopReqDetail";
                opts.ajax = {
                    url: _url,
                    type: "POST",
                    dataSrc: function (json) {

                        if (json.Success == false) {
                            swal(json.Message, '', 'error');
                            return [];
                        }
                        else {
                            return json.Data.Items;
                        }

                    },
                    data: data
                };


                tableDetail = $('#tableDetail').DataTable(opts);
            }



            //明细
            $('.btns-container').on('click', '#btnDetail', function () {
                $('.editTitle').text("查看明细");
                if (tableDetail) {
                    tableDetail.destroy();
                }
                loadTableDetail({ TaskBillNo: _TaskBillNo, OutStockID: _StockID, Type: 1 });
            });


        });

    </script>
</asp:Content>

